/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.smartlink;

import org.smartlink.search.BestMatchEntry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.ViewInflate;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/** Represents a single best match entry in a list. Programmatically constructed. */
public class BestMatchView extends LinearLayout {

	// private static final String TAG = "BestMatchView";
	private TextView target;
	private TextView stats;
	private BestMatchEntry entry;
	private boolean expanded;
	private ImageView icon;

	public BestMatchView(Activity activity, int position, BestMatchEntry entry) {
		super(activity);
		this.entry = entry;
		this.setOrientation(VERTICAL);
		if (entry == null) {
			return;
		}
		Link link = (Link) entry.objPtr;
		int match = entry.sim / 10;
		// horizontal stripes
		if ((position % 2) == 0) {
			this.setBackgroundColor(0x33dadada);
		}

		ViewInflate inflate = (ViewInflate) activity.getSystemService(Context.INFLATE_SERVICE);
		final View bestMatchView = inflate.inflate(R.layout.best_match, null, null);

		TextView key = (TextView) bestMatchView.findViewById(R.id.key_field);
		key.setText(link.key);
		key.setTextSize(14 + (match / 10));

		addView(bestMatchView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		ProgressBar bar = (ProgressBar) bestMatchView.findViewById(R.id.progress_horizontal);

		bar.setPreferredHeight(9 + (match / 10));
		bar.setMax(1000);
		bar.setProgress(entry.sim);
		bar.setSecondaryProgress(0);

		target = (TextView) bestMatchView.findViewById(R.id.target_field);
		stats = (TextView) bestMatchView.findViewById(R.id.stats_field);
		icon = (ImageView) bestMatchView.findViewById(R.id.icon);
		target.setText(link.description);
		stats.setText(Integer.toString(match) + "%");

		expanded = true;
		expandTarget(false);
		retrieveIcon(activity);
		// LinkList list = (LinkList) activity;
		// setOnPopulateContextMenuListener(list);
	}

	private void retrieveIcon(Activity activity) {
		Link link = (Link) entry.objPtr;
		String target = link.target;
		if (target == null) {
			return;
		}
		// Log.w(TAG, "trying to get icon for: " + target);
		Intent intent = new Intent(Intent.VIEW_ACTION, Uri.parse(target));
		PackageManager pm = activity.getPackageManager();
		try {
			Drawable itemIcon = pm.getActivityIcon(intent);
			if (itemIcon != null) {
				// Log.w(TAG, "drawable: " + theIcon);
				icon.setImageDrawable(itemIcon);
				icon.setAdjustViewBounds(true);
				icon.setMaxHeight(32);
				icon.setMaxWidth(32);
			}
		} catch (NameNotFoundException e) {
			// Log.w(TAG, "exception: " + e);
		}
	}

	/** expands the target row if necessary (over threshold or forced) */
	public void expandTarget(boolean forceExpand) {
		// TODO: disabled for now, it is just too slow...
		if (true)
			return;
		if (entry.sim > 300 || forceExpand) {
			if (expanded) {
				return;
			}
			target.setVisibility(VISIBLE);
			stats.setVisibility(VISIBLE);
			// TODO: get height?
			target.setHeight(20);
			stats.setHeight(20);
			expanded = true;
		} else {
			// collapse it
			if (!expanded) {
				return;
			}
			target.setVisibility(INVISIBLE);
			stats.setVisibility(INVISIBLE);
			target.setHeight(2);
			stats.setHeight(2);
			expanded = false;
		}
	}

	public Link getLink() {
		return (Link) entry.objPtr;
	}

}
